#ifndef __CM_NAT_JINGLE_MANAGER_H__
#define __CM_NAT_JINGLE_MANAGER_H__

#include "Poco/Runnable.h"
#include "JingleSession.h"
#include "JingleExtension.h"
#include "Jingle.h"
#include "Poco/TimedNotificationQueue.h"


using Poco::Runnable;
using Poco::TimedNotificationQueue;

namespace cm_nat
{
    class JingleManager;

    class JingleManagerListener
    {
    public:
        virtual void onSessionInitiate(JingleSession* p_js) = 0;
        virtual void onSessionTerminate(JingleSession* p_js) = 0;

    };

    class JingleManager: public Runnable, public JingleListener
    {
    public:
        JingleManager();
        ~JingleManager();

        int setup( ClientBase* p_client );
        int teardown();
        void registerListener(JingleManagerListener* p_listener );

        JingleSession* findJingleSession(std::string sid);

        bool addJingleSession( JingleSession* p_js );
        bool removeJingleSession( JingleSession* p_js );

        void send(IQ &iq);

        //from JingleListener
        void onJingleTag(const Tag* p_tag);

        //derived from Runnable
        void run();

        enum{
            EVENT_INCOMING_JINGLE_TAG
        };

    private:
        bool processJingleTag(const Tag* p_je);

        bool processInitiateAction(const  Tag* p_tag );
        bool processTerminateAction( const Tag* p_tag );
        
        bool processSessionActions( const Tag* p_tag );

    private:
        JID m_jid;
        ClientBase* m_parent;
        bool m_stopFlag; 
        JingleSessionIdMap m_jingleSessions;
        TimedNotificationQueue m_queue;

        Jingle* m_jingleHandler;
        JingleManagerListener* m_listener ;

        Poco::Thread m_thread;
    };
}

#endif